package com.crm.crm.controller;

import com.crm.crm.domain.Activity;
import com.crm.crm.domain.dto.PageQueryParams;
import com.crm.crm.domain.vo.PageResult;
import com.crm.crm.domain.vo.business.result.Result;
import com.crm.crm.service.ActivityService;
import com.crm.crm.util.RandomStringGenerator;
import com.crm.crm.util.UserThreadLocalUtil;
import org.apache.commons.math3.stat.inference.OneWayAnova;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Random;

@RestController
@RequestMapping("/clues/activity")
public class ActivityController {

    @Autowired
    private ActivityService activityService;

    //添加市场活动
    @PostMapping
    public Result add(@RequestBody Activity activity){
        activity.setCreateTime(LocalDateTime.now());
        activity.setUpdateTime(LocalDateTime.now());
        activity.setCreateBy(UserThreadLocalUtil.getCurrentUserName());
        activity.setUpdateBy(UserThreadLocalUtil.getCurrentUserName());
        activity.setCode(RandomStringGenerator.generateRandomString(10));
       activityService.save(activity);
        return Result.success();
    }
    //分页查询
    @GetMapping("/list")
    public Result pageQuery(PageQueryParams pageQueryParams){
        PageResult<Activity> pageResult = activityService.pageQuery(pageQueryParams);
        return Result.success().put("total",pageResult.getTotal()).put("rows",pageResult.getRows());
    }
    //删除市场活动
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id){
        activityService.removeById(id);
        return Result.success();
    }
    //根据id查
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        Activity activity = activityService.getById(id);
        return Result.success().put("data",activity);
    }
    //修改市场活动
    @PutMapping
    public Result update(@RequestBody Activity activity){
        activity.setUpdateTime(LocalDateTime.now());
        activityService.updateById(activity);
        return Result.success();
    }
    //线索页面调用分类
    @GetMapping("/listselect/{channel}")
    public Result listSelect(@PathVariable Integer channel){
       List<Activity> list = activityService.selectListByType(channel);
       return Result.success().put("data",list);
    }
}
